<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\CouponResource;
use App\Models\Coupon;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;

class CouponController extends ApiController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $userId = app('userId');
        $page_size = $request->input('page_size', 20);
        $status = $request->input('status');

        $list = Coupon::where('user_id', $userId)->where('status', $status)
            ->orderBy('id', 'DESC')
            ->paginate($page_size);
        $result = $list->map(function($item) {
            return new CouponResource($item);
        });

        return $this->okReturn([
            'pagination' => [
                'page' => $list->currentPage(),
                'page_size' => intval($page_size),
                'total' => $list->total(),
            ],
            'result' => $result,
        ]);
    }
}
